

package edu.upn.sigemfi.plt.bc;

//import com.sun.org.apache.bcel.internal.generic.Select;
import edu.upn.sigemfi.seg.bc.SucursalFacadeLocal;
import edu.upn.sigemfi.seg.be.Sucursal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SucursalDelegate {



    private SucursalFacadeLocal local;

    public SucursalDelegate() {
        local = lookupSucursalFacade();
    }

    public void crear(Sucursal Sucursal) throws Exception {
        try {
            local.create(Sucursal);
        } catch (Exception e) {
            local = lookupSucursalFacade();
            throw new Exception("Crear Sucursal. " + e.getMessage(), e);
        }
    }

    public void eliminar(Sucursal Sucursal) throws Exception {
        try {
            local.remove(Sucursal);
        } catch (Exception e) {
            local = lookupSucursalFacade();
            throw new Exception("Eliminar Sucursal. " + e.getMessage(), e);
        }
    }

    public void editar(Sucursal Sucursal) throws Exception {
        try {
            local.edit(Sucursal);
        } catch (Exception e) {
            local = lookupSucursalFacade();
            throw new Exception("Editar Sucursal. " + e.getMessage(), e);
        }
    }

    public Sucursal buscar(long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupSucursalFacade();
            throw new Exception("Buscar Sucursal. " + e.getMessage(), e);
        }
    }

    public List<Sucursal> ListarSucursales() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupSucursalFacade();
            throw new Exception("Listar Sucursal. " + e.getMessage(), e);
        }
    }

    public List<Sucursal> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupSucursalFacade();
            throw new Exception("Listar Sucursal. " + e.getMessage(), e);
        }
    }

    private SucursalFacadeLocal lookupSucursalFacade() {
        try {
            Context c = new InitialContext();
            return (SucursalFacadeLocal) c.lookup("java:comp/env/SucursalFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }




}
